<template>
  <gj-loadmore v-if="page!='account'" :bottom-method="loadBottom" ref="loadmore">
    <position-list-sub :myPosition="myPosition"></position-list-sub>
  </gj-loadmore>
  <position-list-sub v-else :myPosition="myPosition" :describe="describe" :page="page"></position-list-sub>
</template>

<script>
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'

  import LoadMore from 'components/LoadMore'
  import PositionListSub from 'components/fund/PositionListSub'

  export default {
    props: {
      page: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        user_code: LocalDAO.getUserCode(),
        myPosition: [],
        start: 0,
        limit: 10,
        describe: '大王，您还没有基金持仓哦！'
      }
    },
    components: {
      'gj-loadmore': LoadMore,
      PositionListSub
    },
    created () {
      if(this.page == 'account'){
        this.getMyPositionAll()
      }
    },
    methods: {
      //上拉加载
      loadBottom () {
        this.getMyPosition();
      },

      //获取我的持仓列表
      getMyPosition () {
        var params = {
          start: this.start,
          limit: this.limit,
          client_id: this.user_code
        };
        RemoteDAO.callJCL11171(params,response => {
          if(response.error == "0"){
            if(response.data.body.holders.length > 0){
              let holders = this.transformData(response.data.body.holders);
              this.myPosition = this.myPosition.concat(holders);
              if(response.data.body.holders.length == this.limit){
                this.start += this.limit;
                this.$refs.loadmore.onBottomLoaded();//加载成功
              }else{
                this.$refs.loadmore.onBottomAllLoaded();//无更多数据
              }
            }else{
              if(this.start == 0){
                this.$refs.loadmore.onBottomError("大王，您还没有基金持仓哦！");//暂无数据
              }else{
                this.$refs.loadmore.onBottomAllLoaded();//无更多数据
              }
            }
          }else{
            this.$refs.loadmore.onBottomError(response.data); //调接口失败
            this.myPosition = [];
          }
        })
      },

      //获取我全部的持仓列表（个人账户总览）
      getMyPositionAll(){
        var params = {
          start: 0,
          limit: 1000,
          client_id: this.user_code
        };
        RemoteDAO.callJCL11171(params,response => {
          if(response.error == "0"){
            if(response.data.body.holders.length > 0){
              this.myPosition = this.transformData(response.data.body.holders);
              this.describe = "";
            }else{
              this.describe = "大王，您还没有基金持仓哦！";
            }
          }else{
            this.myPosition = [];
            this.describe = response.data;
          }
        })
      },

      //数据转换
      transformData (arr) {
        if (arr.length > 0) {
          var result = [];
          for (let i = 0; i < arr.length; i++) {
            var obj = {};
            Object.assign(obj, {
              ofcode: arr[i].ofcode,
              ofname: arr[i].ofname,
              ofstatue: arr[i].ofstatue,
              current_vol: CommonUtil.toFixed(arr[i].current_vol, 2),                  //持仓份额
              enable_vol: CommonUtil.toFixed(arr[i].enable_vol, 2),                    //可赎回份额
              market_balance: CommonUtil.toFixed(arr[i].market_balance, 2),            //基金市值
              last_price: CommonUtil.toFixed(arr[i].last_price, 2),                    //基金净值
              income_balance: this.transformIncome(arr[i].income_balance),             //浮动盈亏
              income_color: Number(CommonUtil.toFixed(arr[i].income_balance,2)) >= 0 ? 'color-red' : 'color-green'   //累计收益显示颜色
            });
            result.push(obj);
          }
          return result;
        } else {
          return [];
        }
      },

      //转换收益率
      transformIncome (income_balance) {
        var income = CommonUtil.toFixed(income_balance, 2);
        if(income >= 0){
          if(income.indexOf('+') == 0){
            return income;
          }else{
            return '+'+income;
          }
        }else{
          if(income.indexOf('-') == 0){
            return income;
          }else{
            return '-'+income;
          }
        }
      }
    }
  }
</script>
